package com.wwdx.controller.admin;

import com.google.common.collect.Lists;
import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.model.dto.BaseActivityDTO;
import com.wwdx.utils.ConstantHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author liguoliang
 * @date 2018-1-28
 */
@Controller
@RequestMapping("/system/wwdx_giveone")
public class WwdxGiveOneController extends BaseController {

    /**
     * 查询列表
     */
    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "rows",defaultValue = "10") Integer rows){

        ModelAndView mav = new ModelAndView();
        String phaseListKey = ConstantHolder.GIVEONE_PHASE_LIST;
        Page redisPages = super.getRedisPages(page, rows);
        List<Object> range = listOperations.range(phaseListKey, redisPages.getPage(), redisPages.getRows());
        List<BaseActivityDTO.GiveOneDTO> giveOneDTOS = Lists.newArrayListWithExpectedSize(range.size());
        for (Object phaseObjKey:range){
            giveOneDTOS.add((BaseActivityDTO.GiveOneDTO) valueOperations.get(phaseObjKey));
        }
        mav.addObject("listSize", listOperations.size(phaseListKey));
        mav.addObject("list", giveOneDTOS);
        return mav;
    }

    /**
     * 赠一得一期数下的书籍列表
     */
    @RequestMapping("/book_list")
    public ModelAndView themebooklist(String phase,
                                      @RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "rows",defaultValue = "10") Integer rows) {

        ModelAndView mav = new ModelAndView();
        String bookListKey = String.format(ConstantHolder.GIVEONE_BOOK_LIST,phase);
        Page redisPages = super.getRedisPages(page, rows);
        List<Object> range = listOperations.range(bookListKey, redisPages.getPage(), redisPages.getRows());
        List<WwdxBookModel> wwdxBookModels = Lists.newArrayListWithExpectedSize(range.size());
        for (Object bookid:range){
            wwdxBookModels.add(wwdxBookService.getByBookId(Long.valueOf(bookid.toString())));
        }
        mav.addObject("phase",phase);
        mav.addObject("listSize", listOperations.size(bookListKey));
        mav.addObject("list", wwdxBookModels);
        return mav;
    }

    /**
     * 新增
     */
    @RequestMapping("/create")
    public ModelAndView create() {
        return new ModelAndView();
    }

    /**
     * 修改页面
     */
    @RequestMapping("/edit")
    public ModelAndView edit(String phase) {
        ModelAndView mav = new ModelAndView();
        String phaseObjKey = String.format(ConstantHolder.GIVEONE_PHASE_OBJ,phase);
        mav.addObject("model",valueOperations.get(phaseObjKey));
        return mav;
    }

    /**
     * 修改保存
     */
    @RequestMapping("/update")
    public String update(BaseActivityDTO.GiveOneDTO dto){
        this.execute(dto);
        return "redirect:/system/wwdx_giveone/list";
    }

    private void execute(BaseActivityDTO.GiveOneDTO dto){
        String phaseListKey = ConstantHolder.GIVEONE_PHASE_LIST;
        String phase = dto.getPhase();
        if (StringUtils.isBlank(phase)){
            phase = super.getUUID();
        }
        String phaseObjKey = String.format(ConstantHolder.GIVEONE_PHASE_OBJ,phase);
        redisTemplate.delete(phaseObjKey);
        dto.setPhase(phase);
        valueOperations.set(phaseObjKey, dto);
        listOperations.rightPush(phaseListKey, phaseObjKey);
    }

    /**
     * 保存新增
     */
    @RequestMapping("/save")
    public String save(BaseActivityDTO.GiveOneDTO dto){
        this.execute(dto);
        return "redirect:/system/wwdx_giveone/list";
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    public String del(String phase){
        String phaseListKey = ConstantHolder.GIVEONE_PHASE_LIST;
        String phaseObjKey = String.format(ConstantHolder.GIVEONE_PHASE_OBJ,phase);
        listOperations.remove(phaseListKey,0,phaseObjKey);
        redisTemplate.delete(phaseObjKey);
        return "redirect:/system/wwdx_giveone/list";
    }
}